package com.ywu.configcenter.refresh;

import com.ywu.common.result.CommonResult;
import org.springframework.beans.BeansException;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ConfigRefreshApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/2/27 22:03
 * @Version 1.0
 **/
@RestController
@RequestMapping("/ywu/config")
public class ConfigRefreshApi implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    /**
    * @Author GroundDemo
    * @Description 刷新配置
    * @Date 23:25 2024/2/27
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/refresh.config.info/{application}")
    public CommonResult<Object> refreshConfig(@PathVariable("application") String application) {
        new Thread() {
            @Override
            public void run() {
                applicationContext.publishEvent(new RefreshEvent(this, null, "Refresh Config"));
            }
        }.start();
        return CommonResult.success(null);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
